package com.woniuxy.processservice.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.DateUtil;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.processclient.client.OrderFeignClient;
import com.woniuxy.processclient.client.ProductFeignClient;
import com.woniuxy.processclient.client.dto.OrderDto;
import com.woniuxy.processclient.client.dto.TeamProductDto;
import com.woniuxy.processdao.mapper.TeacardMapper;
import com.woniuxy.processdao.model.Teacard;
import com.woniuxy.processmodel.dto.TeaCardDto;
import com.woniuxy.processmodel.form.UpdateTeacardForm;
import com.woniuxy.processmodel.form.VenueLookTeacardForm;
import com.woniuxy.processservice.exception.ProcessException;
import com.woniuxy.processservice.service.TeacardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 团课流程表 服务实现类
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@Service
public class TeacardServiceImpl extends ServiceImpl<TeacardMapper, Teacard> implements TeacardService {

    @Autowired
    private TeacardMapper teacardMapper;
    @Resource
    private OrderFeignClient orderFeignClient;
    @Resource
    private ProductFeignClient productFeignClient;
    @Override
    public void addTeacard(String orderNum) {
        QueryWrapper<Teacard> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num", orderNum);
        Teacard teacard = teacardMapper.selectOne(wrapper);
        if(!BeanUtil.isEmpty(teacard)){
            throw new ProcessException(7001,"该团课订单进度已存在");
        }
        OrderDto orderDto = orderFeignClient.findByOrderNum(orderNum).getData();
        if(BeanUtil.isEmpty(orderDto)){
            throw new ProcessException(7009,"该订单不存在");
        }
        TeamProductDto productDto = productFeignClient.findTeamProductById(orderDto.getProductId()).getData();
        Teacard teacard1 = new Teacard();
        teacard1.setOrderNum(orderNum);
        teacard1.setUserId(orderDto.getUserId());
        teacard1.setVenueId(orderDto.getVenueId());
        teacard1.setTeamHour(productDto.getCourseCount());
        teacard1.setTeacardUse(0);
        teacardMapper.insert(teacard1);
    }

    @Override
    public void updateTeacard(UpdateTeacardForm form, Integer venueId) {
        QueryWrapper<Teacard> wrapper = new QueryWrapper<>();
        wrapper.in("order_num",form.getOrderNums());
        List<Teacard> list = teacardMapper.selectList(wrapper);
        for (Teacard teacard : list) {
            if(BeanUtil.isEmpty(teacard)){
                throw new ProcessException(7002,"该团课订单进度不存在");
            }
            if(teacard.getVenueId()!=venueId){
                throw new ProcessException(7003,"该团课订单商家信息异常");
            }
            if(teacard.getTeacardUse()==teacard.getTeamHour()){
                throw new ProcessException(7004,"该团课订单已完成");
            }
            Result<OrderDto> orderDtoResult = orderFeignClient.findByOrderNum(teacard.getOrderNum());
            OrderDto orderDto = orderDtoResult.getData();
            if(orderDto.getOrderState()!=1){
                throw new ProcessException(7005,"该团课订单状态异常");
            }
            teacard.setTeacardUse(teacard.getTeacardUse()+1);
        }
        saveBatch(list);
        for (Teacard teacard : list) {
            if(teacard.getTeacardUse()==teacard.getTeamHour()){
                //当用时等于总课时
                teacard.setCompleteTime(DateUtil.dateTimeToString(new Date()));
                teacardMapper.updateById(teacard);
                //修改团课订单状态为已完成
                orderFeignClient.updateOrderStateByOrderNum(teacard.getOrderNum(),3);
            }
        }
    }

    @Override
    public Teacard lookTeacard(Integer teacardId) {
        Teacard teacard = teacardMapper.selectById(teacardId);
        return teacard;
    }

    @Override
    public List<TeaCardDto> userLookPricard(String orderNum, Integer userId) {
        QueryWrapper<Teacard> wrapper = new QueryWrapper<>();
        wrapper.like(!BeanUtil.isEmpty(orderNum),"order_num",orderNum);
        wrapper.eq("user_id",userId);
        List<Teacard> list = teacardMapper.selectList(wrapper);
        List<TeaCardDto> dtos = BeanUtil.copyToList(list, TeaCardDto.class);
        return dtos;
    }

    @Override
    public PageInfo<TeaCardDto> venueLookTeacard(VenueLookTeacardForm form, Integer venueId) {
        QueryWrapper<Teacard> wrapper = new QueryWrapper<>();
        wrapper.like(!BeanUtil.isEmpty(form.getOrderNum()),"order_num",form.getOrderNum());
        wrapper.eq(!BeanUtil.isEmpty(form.getUserId()),"user_id",form.getUserId());
        wrapper.eq("venue_id",venueId);
        Page<Teacard> page = new Page<>(form.getPageNum(), form.getPageSize());
        page(page,wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(BeanUtil.copyToList(page.getRecords(),TeaCardDto.class));
        return pageInfo;
    }
}
